4 * @author Adam Shorland
7 class JsonContentTest
extends MediaWikiLangTestCase
{
9 protected function setUp() {
12 $this->setMwGlobals( 'wgWellFormedXml', true );
15 public static function provideValidConstruction() {
17 array( 'foo', false, null ),
18 array( '{}', true, (object)array() ),
19 array( '{ "0": "bar" }', true, (object)array( 'bar' ) ),
24 * @dataProvider provideValidConstruction
26 public function testIsValid( $text, $isValid, $expected ) {
27 $obj = new JsonContent( $text, CONTENT_MODEL_JSON
);
28 $this->assertEquals( $isValid, $obj->isValid() );
29 $this->assertEquals( $expected, $obj->getData()->getValue() );
32 public static function provideDataToEncode() {
35 // Round-trip empty array
40 // Round-trip empty object
45 // Round-trip empty array/object (nested)
46 '{ "foo": {}, "bar": [] }',
47 "{\n \"foo\": {},\n \"bar\": []\n}",
51 "{\n \"foo\": \"bar\"\n}",
55 "{\n \"foo\": 1000\n}",
58 '{ "foo": 1000, "0": "bar" }',
59 "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
65 * @dataProvider provideDataToEncode
67 public function testBeautifyJson( $input, $beautified ) {
68 $obj = new JsonContent( $input );
69 $this->assertEquals( $beautified, $obj->beautifyJSON() );
73 * @dataProvider provideDataToEncode
75 public function testPreSaveTransform( $input, $transformed ) {
76 $obj = new JsonContent( $input );
77 $newObj = $obj->preSaveTransform( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() );
78 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
81 private function getMockTitle() {
82 return $this->getMockBuilder( 'Title' )
83 ->disableOriginalConstructor()
87 private function getMockUser() {
88 return $this->getMockBuilder( 'User' )
89 ->disableOriginalConstructor()
92 private function getMockParserOptions() {
93 return $this->getMockBuilder( 'ParserOptions' )
94 ->disableOriginalConstructor()
98 public static function provideDataAndParserText() {
102 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr></tbody></table>'
105 (object)array( 'foo' ),
106 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr></tbody></table>'
109 (object)array( 'foo', 'bar' ),
110 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
112 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
115 (object)array( 'baz' => 'foo', 'bar' ),
116 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
118 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
121 (object)array( 'baz' => 1000, 'bar' ),
122 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
124 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
127 (object)array( '<script>alert("evil!")</script>'),
128 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"<script>alert("evil!")</script>"</td></tr></tbody></table>',
134 * @dataProvider provideDataAndParserText
136 public function testFillParserOutput( $data, $expected ) {
137 $obj = new JsonContent( FormatJson
::encode( $data ) );
138 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
139 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
140 $this->assertEquals( $expected, $parserOutput->getText() );